home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Games / NetHack 3.1.3 / source / sys / mac / mactopl.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-08-01  |  8.2 KB  |  439 lines  |  [TEXT/R*ch]

  1. /*    SCCS Id: @(#)mactopl.c    3.1    91/07/23
  2. /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
  3. /* NetHack may be freely redistributed.  See license for details. */
  4.  
  5. #include "hack.h"
  6. #include <Dialogs.h>
  7. #include <OSUtils.h>
  8. #include <Packages.h>
  9.  
  10. // char FDECL(yn_function,(const char *, const char *, CHAR_P));
  11.  
  12. int FDECL ( try_key_queue , ( char * ) ) ;
  13.  
  14. extern void SetFrameItem ( DialogPtr , short , short ) ;
  15. extern void FlashButton ( DialogPtr , short ) ;
  16.  
  17. extern char * PtoCstr ( unsigned char * ) ;
  18. extern unsigned char * CtoPstr ( char * ) ;
  19.  
  20. void FDECL ( enter_topl_mode , ( char * ) ) ;
  21. void FDECL ( leave_topl_mode , ( char * ) ) ;
  22. void FDECL ( topl_set_resp , ( char * , char ) ) ;
  23.  
  24. extern winid inSelect ;
  25. extern short frame_corner ;
  26.  
  27.  
  28. char
  29. queued_resp(char *resp)
  30. {
  31.     char buf[30];
  32.     if (try_key_queue(buf)) {
  33.         if (!resp || strchr(resp, buf[0]))
  34.             return buf[0];
  35.         if (digit(buf[0]) && strchr(resp, '#')) {
  36.             yn_number = atoi(buf);
  37.             return '#';
  38.         }
  39.     }
  40.     return '\0';
  41. }
  42.  
  43.  
  44. #define YN_DLOG 133
  45. #define YNQ_DLOG 134
  46. #define YNAQ_DLOG 135
  47. #define YNNAQ_DLOG 136
  48.  
  49. static int yn_user_item [ ] = { 5 , 6 , 7 , 8 } ;
  50. static short gEnterItem , gEscItem ;
  51. static const char * gRespStr = NULL ;
  52. static char gDef = 0 ;
  53. static short dlogID ;
  54.  
  55.  
  56. static void
  57. SetEnterItem ( DialogPtr dp , const short newEnterItem )
  58. {
  59.     short kind ;
  60.     Handle item ;
  61.     Rect r , r2 ;
  62.  
  63.     if ( gEnterItem != newEnterItem ) {
  64.  
  65.         GetDItem ( dp , gEnterItem , & kind , & item , & r2 ) ;
  66.         InsetRect ( & r2 , - 4 , - 4 ) ;
  67.         EraseRect ( & r2 ) ;
  68.         InvalRect ( & r2 ) ;
  69.  
  70.         gEnterItem = newEnterItem ;
  71.  
  72.         GetDItem ( dp , newEnterItem , & kind , & item , & r2 ) ;
  73.         frame_corner = kind == ctrlItem + btnCtrl ? 16 : 0 ;
  74.         InsetRect ( & r2 , - 4 , - 4 ) ;
  75.         InvalRect ( & r2 ) ;
  76.         r = r2 ;
  77.         GetDItem ( dp , yn_user_item [ dlogID - YN_DLOG ] , & kind , & item , & r2 ) ;
  78.         SetDItem ( dp , yn_user_item [ dlogID - YN_DLOG ] , kind , item , & r ) ;
  79.     }
  80. }
  81.  
  82.  
  83. static void
  84. do_tabbing ( DialogPtr dp )
  85. {
  86.     SetEnterItem(dp, gEnterItem == 1 ? strlen(gRespStr) : gEnterItem - 1);
  87. }
  88.  
  89.  
  90. static void
  91. set_yn_number(DialogPtr dp)
  92. {
  93.     if (gRespStr && gRespStr[gEnterItem-1] == '#') {
  94.         short k;
  95.         Handle h;
  96.         Rect r;
  97.         Str255 s;
  98.         GetDItem(dp, gEnterItem, &k, &h, &r);
  99.         GetIText(h, s);
  100.         if (s[0])
  101.             StringToNum(s, &yn_number);
  102.     }
  103. }
  104.  
  105.  
  106. pascal Boolean
  107. YNAQFilter ( DialogPtr dp , EventRecord * ev , short * itemHit )
  108. {
  109.     unsigned char code ;
  110.     char ch ;
  111.     char * re = (char *) gRespStr ;
  112.  
  113.     if ( ev -> what != keyDown ) {
  114.  
  115.         return 0 ;
  116.     }
  117.     code = ( ev -> message & 0xff00 ) >> 8 ;
  118.     ch = ev -> message & 0xff ;
  119.  
  120.     switch ( code ) {
  121.  
  122.     case 0x24 :
  123.     case 0x4c :
  124.         set_yn_number ( dp ) ;
  125.         * itemHit = gEnterItem ;
  126.         FlashButton ( dp , * itemHit ) ;
  127.         return 1 ;
  128.  
  129.     case 0x35 :
  130.     case 0x47 :
  131.         * itemHit = gEscItem ;
  132.         FlashButton ( dp , * itemHit ) ;
  133.         return 1 ;
  134.  
  135.     case 0x30 :
  136.         do_tabbing ( dp ) ;
  137.         return 0 ;
  138.     }
  139.     switch ( ch ) {
  140.  
  141.     case '\r' :
  142.     case '\n' :
  143.     case ' ' :
  144.     case 3 :
  145.         set_yn_number ( dp ) ;
  146.         * itemHit = gEnterItem ;
  147.         FlashButton ( dp , * itemHit ) ;
  148.         return 1 ;
  149.  
  150.     case 9 :
  151.         do_tabbing ( dp ) ;
  152.         return 0 ;
  153.  
  154.     case 27 :
  155.         * itemHit = gEscItem ;
  156.         FlashButton ( dp , * itemHit ) ;
  157.         return 1 ;
  158.  
  159.     case CHAR_BS :
  160.     case 28 : case 29 : case 30 : case 31 : /* the four arrow keys */
  161.     case '0' : case '1' : case '2' : case '3' : case '4' :
  162.     case '5' : case '6' : case '7' : case '8' : case '9' :
  163.     {    char * loc = strchr ( gRespStr , '#' ) ;
  164.         if ( loc ) {
  165.             SetEnterItem( dp , loc - gRespStr + 1 ) ;
  166.             return 0; /* Dialog Manager will then put this key into the text field. */
  167.         }
  168.     }
  169.     }
  170.  
  171.     while ( * re ) {
  172.  
  173.         if ( * re == ch ) {
  174.  
  175.             * itemHit = ( re - gRespStr ) + 1 ;
  176.             FlashButton ( dp , * itemHit ) ;
  177.             return 1 ;
  178.         }
  179.         re ++ ;
  180.     }
  181.  
  182.     nhbell ( ) ;
  183.     ev -> what = nullEvent ;
  184.     return 0 ;
  185. }
  186.  
  187.  
  188. static char
  189. do_question_dialog ( char * query , int dlog , int defbut , char * resp )
  190. {
  191.     Str255 p ;
  192.     DialogPtr dp ;
  193.     short item ;
  194.  
  195.     char c = queued_resp ( (char *) resp ) ;
  196.     if ( c )
  197.         return c ;
  198.  
  199.     dlogID = dlog ;
  200.     strcpy ( (char *) p , query ) ;
  201.     ParamText ( CtoPstr ( (char *) p ) , (uchar *) NULL , (uchar *) NULL , (uchar *) NULL ) ;
  202.     dp = mv_get_new_dialog ( dlog ) ;
  203.     if ( ! dp ) {
  204.  
  205.         return 0 ;
  206.     }
  207.     SetPort ( dp ) ;
  208.     ShowWindow ( dp ) ;
  209.  
  210.     gEscItem = strlen ( resp ) ;
  211.     gEnterItem = defbut ;
  212.     gRespStr = resp ;
  213.  
  214.     SetFrameItem ( dp , yn_user_item [ dlogID - YN_DLOG ] , gEnterItem ) ;
  215.  
  216.     InitCursor ( ) ;
  217.     mv_modal_dialog ( YNAQFilter , & item ) ;
  218.     mv_close_dialog ( dp ) ;
  219.     return resp [ item - 1 ] ;
  220. }
  221.  
  222.  
  223. pascal Boolean
  224. OneCharDLOGFilter ( DialogPtr dp , EventRecord * ev , short * item )
  225. {
  226.     char ch ;
  227.     short k ;
  228.     Handle h ;
  229.     Rect r ;
  230.     unsigned char com [ 2 ] ;
  231.  
  232.     if ( ev -> what != keyDown ) {
  233.  
  234.         return 0 ;
  235.     }
  236.     ch = ev -> message & 0xff ;
  237.  
  238.     com [ 0 ] = 1 ;
  239.     com [ 1 ] = ch ;
  240.  
  241.     if ( ch == 27 ) {
  242.  
  243.         GetDItem ( dp , 4 , & k , & h , & r ) ;
  244.         SetIText ( h , com ) ;
  245.         * item = 2 ;
  246.         FlashButton ( dp , 2 ) ;
  247.         return 1 ;
  248.     }
  249.     if ( ! gRespStr || strchr ( gRespStr , ch ) ) {
  250.  
  251.         GetDItem ( dp , 4 , & k , & h , & r ) ;
  252.         SetIText ( h , com ) ;
  253.         * item = 1 ;
  254.         FlashButton ( dp , 1 ) ;
  255.         return 1 ;
  256.     }
  257.     if ( ch == 10 || ch == 13 || ch == 3 || ch == 32 ) {
  258.  
  259.         com [ 1 ] = gDef ;
  260.         GetDItem ( dp , 4 , & k , & h , & r ) ;
  261.         SetIText ( h , com ) ;
  262.         * item = 1 ;
  263.         FlashButton ( dp , 1 ) ;
  264.         return 1 ;
  265.     }
  266.     if ( ch > 32 && ch < 127 ) {
  267.  
  268.         GetDItem ( dp , 4 , & k , & h , & r ) ;
  269.         SetIText ( h , com ) ;
  270.         * item = 1 ;
  271.         FlashButton ( dp , 1 ) ;
  272.         return 1 ;
  273.     }
  274.     nhbell ( ) ;
  275.     ev -> what = nullEvent ;
  276.     return 1 ;
  277. }
  278.  
  279.  
  280. static char
  281. generic_yn_function ( query , resp , def )
  282. const char * query , * resp ;
  283. char def ;
  284. {
  285.     DialogPtr dp ;
  286.     short k , item ;
  287.     Handle h ;
  288.     Rect r ;
  289.     unsigned char com [ 32 ] = { 1 , 27 } ; // margin for getitext
  290.     Str255 pQuery ;
  291.  
  292.     char c = queued_resp ( (char *) resp ) ;
  293.     if ( c )
  294.         return c ;
  295.  
  296.     dp = mv_get_new_dialog ( 137 ) ;
  297.     if ( ! dp ) {
  298.  
  299.         return 0 ;
  300.     }
  301.     SetPort ( dp ) ;
  302.     ShowWindow ( dp ) ;
  303.     InitCursor ( ) ;
  304.     SetFrameItem ( dp , 6 , 1 ) ;
  305.     if ( def ) {
  306.  
  307.         com [ 1 ] = def ;
  308.     }
  309.     strcpy ( ( char * ) pQuery , query ) ;
  310.     if ( resp && * resp ) {
  311.  
  312.         strcat ( ( char * ) pQuery , " (" ) ;
  313.         strcat ( ( char * ) pQuery , resp ) ;
  314.         strcat ( ( char * ) pQuery , ")" ) ;
  315.     }
  316.     ParamText ( CtoPstr ( (char *) pQuery ) , (uchar *) NULL , (uchar *) NULL , (uchar *) NULL ) ;
  317.     GetDItem ( dp , 4 , & k , & h , & r ) ;
  318.     SetIText ( h , com ) ;
  319.     SelIText ( dp , 4 , 0 , 0x7fff ) ;
  320.     InitCursor ( ) ;
  321.     SetFrameItem ( dp , 6 , 1 ) ;
  322.     gRespStr = resp ;
  323.     gDef = def ;
  324.     do {
  325.  
  326.         mv_modal_dialog ( OneCharDLOGFilter , & item ) ;
  327.  
  328.     } while ( item != 1 && item != 2 ) ;
  329.     GetIText ( h , com ) ;
  330.  
  331.     mv_close_dialog ( dp ) ;
  332.     if ( item == 2 || ! com [ 0 ] ) {
  333.  
  334.         return 27 ; // escape
  335.     }
  336.     return com [ 1 ] ;
  337. }
  338.  
  339.  
  340. static char
  341. ynaq_dialog ( query , resp , def )
  342. const char * query , * resp ;
  343. char def ;
  344. {
  345.     int dia = 0 ;
  346.  
  347.     if ( resp ) {
  348.  
  349.         if ( ! strcmp ( resp , ynchars ) ) {
  350.  
  351.             dia = YN_DLOG ;
  352.         }
  353.         if ( ! strcmp ( resp , ynqchars ) ) {
  354.  
  355.             dia = YNQ_DLOG ;
  356.         }
  357.         if ( ! strcmp ( resp , ynaqchars ) ) {
  358.  
  359.             dia = YNAQ_DLOG ;
  360.         }
  361.         if ( ! strcmp ( resp , ynNaqchars ) ) {
  362.  
  363.             dia = YNNAQ_DLOG ;
  364.         }
  365.     }
  366.     if ( ! dia ) {
  367.  
  368.         return generic_yn_function ( query , resp , def ) ;
  369.     }
  370.  
  371.     return do_question_dialog ( (char *) query , dia ,
  372.         ( strchr ( resp , def ) - resp ) + 1 , (char *) resp ) ;
  373. }
  374.  
  375.  
  376. char
  377. topl_yn_function(query,resp, def)
  378. const char *query,*resp;
  379. char def;
  380. {
  381.     char buf[30];
  382.     char c = queued_resp((char *) resp);
  383.     if (!c) {
  384.         enter_topl_mode((char *) query);
  385.         topl_set_resp((char *) resp, def);
  386.  
  387.         do {
  388.             c = readchar();
  389.             if (c && resp && !strchr(resp, c)) {
  390.                 nhbell();
  391.                 c = '\0';
  392.             }
  393.         } while (!c);
  394.  
  395.         topl_set_resp("", '\0');
  396.         leave_topl_mode(buf);
  397.         if (c == '#')
  398.             yn_number = atoi(buf);
  399.     }
  400.     return c;
  401. }
  402.  
  403.  
  404. char
  405. popup_yn_function(query,resp, def)
  406. const char *query,*resp;
  407. char def;
  408. {
  409.     char ch [ 2 ] ;
  410.  
  411.     if ( ch [ 0 ] = ynaq_dialog ( query , resp , def ) ) {
  412.  
  413.         return ch [ 0 ] ;
  414.     }
  415.  
  416.     return topl_yn_function(query, resp, def);
  417. }
  418.  
  419.  
  420. char
  421. mac_yn_function(query,resp, def)
  422. const char *query,*resp;
  423. char def;
  424. /*
  425.  *   Generic yes/no function. 'def' is the default (returned by space or
  426.  *   return; 'esc' returns 'q', or 'n', or the default, depending on
  427.  *   what's in the string. The 'query' string is printed before the user
  428.  *   is asked about the string.
  429.  *   If resp is NULL, any single character is accepted and returned.
  430.  */
  431. {
  432.     if (flags.popup_dialog)
  433.         return popup_yn_function(query, resp, def);
  434.     else
  435.         return topl_yn_function(query, resp, def);
  436. }
  437.  
  438. /*topl.c*/
  439.